﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NorthwindDAL;
using NorthwindDAL.Repositories;
using NorthwindModel.Models;

namespace NorthwindService
{
    /// <summary>
    /// Implement this Service as Bussiness Module, contains Properties implement all Repositories needed and one UnitOfWork
    /// All Repositories use the same UnitOfWork as Context
    /// </summary>
    public partial class CustomerService: IDisposable
    {
        #region Private Variables
        private UnitOfWork context = new UnitOfWork();
        private CustomerRepository customerRepository;      
        //private OrderRepository orderRepository;
        private bool dsiposed = false;
        #endregion

        #region Constructor

        public CustomerService()
        {
            customerRepository = context.CustomerRepository;
            //orderRepository = new OrderRepository(context);
        }

        #endregion

        #region Public Properties

        //public CustomerRepository CustomerRepository
        //{
        //    get 
        //    {
        //        if (customerRepository == null)
        //            customerRepository = new CustomerRepository(context);
        //        return customerRepository; 
        //    }
        //}

        //public OrderRepository OrderRepository
        //{
        //    get 
        //    {
        //        if (orderRepository == null)
        //            orderRepository = new OrderRepository(context);
        //        return orderRepository; 
        //    }
        //}

        #endregion

        #region Public Functions

        public void SaveCustomer(Customer customer)
        {
            customerRepository.Save(customer);
            int result = customerRepository.Commit();
        }

        public Customer GetCustomerByID(string customerID)
        {
            Customer customerFound = customerRepository.FindCustomerByID(customerID);
            return customerFound;
        }

        #endregion

        protected virtual void Dispose(bool dispsing)
        {
            if (!this.dsiposed)
            {
                if (dispsing)
                {
                    context.Dispose();
                }
                this.dsiposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
